package com.wehealth.adapter;

import java.util.List;

import com.wehealth.R;
import com.wehealth.dm.DM_Questionnaire_page2_item;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class Adapter_questionnaire_page2 extends BaseAdapter{
	
	private Context ctx;
	private List<DM_Questionnaire_page2_item> mList;
	private boolean  isAddNewItem = true;
	
	public Adapter_questionnaire_page2(Context context,List<DM_Questionnaire_page2_item> mList){
		ctx =context;
		this.mList =mList;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int pos) {
		// TODO Auto-generated method stub
		return pos;
	}

	@Override
	public long getItemId(int pos) {
		// TODO Auto-generated method stub
		return pos;
	}

	@Override
	public View getView(int pos, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		final int index = pos;
		View view = LayoutInflater.from(ctx).inflate(R.layout.questionnaire_page2_list_item,null);
		
		TextView title = (TextView)view.findViewById(R.id.questionnaire_page2_item_name);
		mList.get(index).getTitle();
		title.setText(mList.get(index).getTitle());
		
		RadioGroup  radioGroup =(RadioGroup)view.findViewById(R.id.questionnaire_page2_item_radiogroup);
		int checkValue = mList.get(index).getCheckValue();
		int  checkID = 0;
		switch (checkValue) {
		case DM_Questionnaire_page2_item.CHECK_NO:
			checkID =R.id.radio_no;
			break;
		case DM_Questionnaire_page2_item.CHECK_YES:
			checkID =R.id.radio_yes;
			break;
		case DM_Questionnaire_page2_item.CHECK_UNKNOW:
			checkID =R.id.radio_unknow;
			break;
		}
		radioGroup.check(checkID);
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup arg0, int id) {
				// TODO Auto-generated method stub
				switch (id) {
				case R.id.radio_no:
					mList.get(index).setCheckValue(DM_Questionnaire_page2_item.CHECK_NO);
					if(index ==1 && !isAddNewItem){
						isAddNewItem =true;
						mList.remove(2);
						notifyDataSetChanged();
					}
					break;
				case R.id.radio_yes:
					mList.get(index).setCheckValue(DM_Questionnaire_page2_item.CHECK_YES);
					if(index ==1 && isAddNewItem){
						isAddNewItem =false;
						DM_Questionnaire_page2_item item = new DM_Questionnaire_page2_item();
						item.setTitle(ctx.getString(R.string.questionaire_page2_item_diabetes2));
						item.isdiabetes2 = true;
						mList.add(2, item);
						notifyDataSetChanged();
					}
					break;
				case R.id.radio_unknow:
					mList.get(index).setCheckValue(DM_Questionnaire_page2_item.CHECK_UNKNOW);
					if(index ==1 && !isAddNewItem){
						isAddNewItem =true;
						mList.remove(2);
						notifyDataSetChanged();
					}
					break;

				default:
					break;
				}
				
			}
		});
		return view;
	}

}
